﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DTO;
using BestBuy.DAL;
using BestBuy.Common;
namespace BestBuy.PageUser
{
    public partial class AddNewSellOffer : System.Web.UI.Page
    {
        public void Page_Load(object sender, EventArgs e)
        {
            if (Session["IDDN"].ToString().Equals(""))
            {
                Response.Redirect("~/Account/Login.aspx");
            }
            if (!IsPostBack)
            {
                loadDonVi();
            }
        }
        const int MaxQuantity = 3;
        private void loadDonVi()
        {
            DDdonvi1.DataSource = cDonVi.getDonVi();
            DDdonvi1.DataTextField = "TenDonVi";
            DDdonvi1.DataValueField = "IDDonVi";
            DDdonvi1.DataBind();

            DDDonvi2.DataSource = cDonVi.getDonVi();
            DDDonvi2.DataTextField = "TenDonVi";
            DDDonvi2.DataValueField = "IDDonVi";
            DDDonvi2.DataBind();
        }
        protected void btSubmit_Click(object sender, EventArgs e)
        {
            BangTinBan bangtin = new BangTinBan();
            bangtin.TieuDe = tbSubject.Text;
            bangtin.TuKhoa = tbKeyWord.Text;
            DateTime dcreate = DateTime.Now;
            bangtin.NgayDang = dcreate.ToShortDateString();
            bangtin.MoTa = tbDescription.Text;
            dcreate = dcreate.AddDays(int.Parse(DropDownList1.SelectedItem.Value));
            bangtin.ThoiHanDang = dcreate.ToShortDateString();
            if (UploadPhoto.HasFile)
            {
                String str = Server.MapPath("~/Images/Product/") + UploadPhoto.FileName;
                UploadPhoto.SaveAs(str);
                bangtin.HinhAnh = UploadPhoto.FileName;
            }
            else
            {
                bangtin.HinhAnh = "";
            }
            bangtin.KyHanGiaoHang = tbDeliverTime.Text;
            bangtin.KhaNangCungCap = tbSupply.Text + "/" + DDdonvi1.SelectedItem.Text + "/" + DDdate.SelectedItem.Text;
            bangtin.NoiXuatXu = tbPlace.Text;
            bangtin.SoLuongDatToiThieu = tbMiniOrder.Text + "/" + DDDonvi2.SelectedItem.Text;
            int[] payment = { 0, 0, 0, 0 };
            for (int i = 0; i < CheckBoxList1.Items.Count;i++ )
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    payment[i] = 1;
                }
            }
            for (int i = 0; i < payment.Count();i++ )
            {
                bangtin.PhuongThucThanhToan = bangtin.PhuongThucThanhToan + payment[i].ToString() +"+";
            }
            bangtin.IDTaiKhoan = int.Parse(Session["IDDN"].ToString());
            bangtin.TinhTrang = (int)BangTinBanStatus.Unverified;
            cBangTinBan.AddSellOffer(bangtin);
            int idbangtin = cBangTinBan.getIdBangtin(int.Parse(Session["IDDN"].ToString()));
            for(int i=0;i < CategoryListBox.Items.Count;i++)
            {
                DanhMucBan dmb = new DanhMucBan();
                dmb.IDBangTinBan = idbangtin;
                dmb.IDDanhMuc = int.Parse(CategoryListBox.Items[i].Value);
                cBangTinBan.AddCategorySell(dmb);
            }
            Response.Redirect("ManageSellOffer.aspx");
        }
        protected void lbtnChangeCategory_Click(object sender, EventArgs e)
        {
            LoadCategoriesIntoListBox(gvLevel1, null);
            lbLimitQuantity.Text = MaxQuantity - CategoryListBox.Items.Count + "";
            this.mpeDanhMuc.Show();
        }
        protected void gvLevel1_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            LinkButton hidIDDanhMuc = (LinkButton)e.Row.FindControl("LinkDanhMuc");
            if (hidIDDanhMuc != null)
            {
                int id = Int32.Parse(hidIDDanhMuc.Attributes["ObjectId"]);
                if (!CCongTy.HasChildren(id))
                {
                    CheckBox checkbox = (CheckBox)e.Row.FindControl("cbLevel");
                    Label lb = (Label)e.Row.FindControl("lbLevel");
                    if (checkbox != null && lb != null)
                    {
                        checkbox.Visible = true;
                        lb.Visible = false;
                    }
                }
            }
        }

        protected void gvLevel2_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            LinkButton hidIDDanhMuc = (LinkButton)e.Row.FindControl("LinkDanhMuc");
            if (hidIDDanhMuc != null)
            {
                int id = Int32.Parse(hidIDDanhMuc.Attributes["ObjectId"]);
                if (!CCongTy.HasChildren(id))
                {
                    CheckBox checkbox = (CheckBox)e.Row.FindControl("cbLevel");
                    Label lb = (Label)e.Row.FindControl("lbLevel");
                    if (checkbox != null && lb != null)
                    {
                        checkbox.Visible = true;
                        lb.Visible = false;
                    }
                }
            }
        }

        protected void gvLevel3_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            LinkButton hidIDDanhMuc = (LinkButton)e.Row.FindControl("LinkDanhMuc");
            if (hidIDDanhMuc != null)
            {
                int id = Int32.Parse(hidIDDanhMuc.Attributes["ObjectId"]);
                if (!CCongTy.HasChildren(id))
                {
                    CheckBox checkbox = (CheckBox)e.Row.FindControl("cbLevel");
                    Label lb = (Label)e.Row.FindControl("lbLevel");
                    if (checkbox != null && lb != null)
                    {
                        checkbox.Visible = true;
                        lb.Visible = false;
                    }
                }
            }
        }
        protected void cbLevel_CheckedChanged(object sender, EventArgs e)
        {
            this.mpeDanhMuc.Show();
        }
        protected void LinkDanhMucLevel1_Click(object sender, EventArgs e)
        {
            LinkButton senderButton = sender as LinkButton;
            int id = 0;
            if (Int32.TryParse(senderButton.Attributes["ObjectId"], out id))
            {
                gvLevel3.DataSource = null;
                gvLevel3.DataBind();
                LoadCategoriesIntoListBox(gvLevel2, id);
            }
            this.mpeDanhMuc.Show();
        }

        protected void LinkDanhMucLevel2_Click(object sender, EventArgs e)
        {
            LinkButton senderButton = sender as LinkButton;
            int id = 0;
            if (Int32.TryParse(senderButton.Attributes["ObjectId"], out id))
            {
                LoadCategoriesIntoListBox(gvLevel3, id);
            }
            this.mpeDanhMuc.Show();
        }

        protected void LinkDanhMucLevel3_Click(object sender, EventArgs e)
        {
            this.mpeDanhMuc.Show();
        }
        private bool LoadCategoriesIntoListBox(GridView control, int? parentCatID)
        {
            var cats = CCongTy.GetChildrenOfCategory(parentCatID);
            control.DataSource = cats;
            control.DataBind();
            return cats != null && cats.Count > 0;
        }
        private bool ExistCategoryInList(int IDCategory)
        {
            foreach (ListItem item in CategoryListBox.Items)
            {
                if (int.Parse(item.Value) == IDCategory)
                    return true;
            }
            return false;
        }

        private void AddCategoryToList(int IDCategory)
        {
            if (!ExistCategoryInList(IDCategory))
            {
                DanhMuc dm = CCongTy.GetCategoryByID(IDCategory);
                ListItem item = new ListItem();
                item.Value = dm.IDDanhMuc.ToString();
                item.Text = dm.TenDanhMuc;
                CategoryListBox.Items.Add(item);
            }
        }
        protected void btnAddCategory_OnClick(object sender, EventArgs e)
        {
            try
            {
                int id = 0;
                for (int i = 0; i < gvLevel1.Rows.Count; i++)
                {
                    if (CategoryListBox.Items.Count == MaxQuantity)
                    {
                        return;
                    }
                    CheckBox chk = (CheckBox)gvLevel1.Rows[i].FindControl("cbLevel");
                    if (chk != null)
                    {
                        if (chk.Checked == true && chk.Enabled == true)
                        {
                            id = int.Parse(chk.Attributes["ObjectId"]);
                            AddCategoryToList(id);
                        }
                    }
                }
                for (int i = 0; i < gvLevel2.Rows.Count; i++)
                {
                    if (CategoryListBox.Items.Count == MaxQuantity)
                    {
                        return;
                    }
                    CheckBox chk = (CheckBox)gvLevel2.Rows[i].FindControl("cbLevel");
                    if (chk != null)
                    {
                        if (chk.Checked == true && chk.Enabled == true)
                        {
                            id = int.Parse(chk.Attributes["ObjectId"]);
                            AddCategoryToList(id);
                        }
                    }
                }
                for (int i = 0; i < gvLevel3.Rows.Count; i++)
                {
                    if (CategoryListBox.Items.Count == MaxQuantity)
                    {
                        return;
                    }
                    CheckBox chk = (CheckBox)gvLevel3.Rows[i].FindControl("cbLevel");
                    if (chk != null)
                    {
                        if (chk.Checked == true && chk.Enabled == true)
                        {
                            id = int.Parse(chk.Attributes["ObjectId"]);
                            AddCategoryToList(id);
                        }
                    }
                }
                lbThongBaoAddCategory.Text = "Add categories success.";
                lbThongBaoAddCategory.CssClass = "success";
                lbThongBaoAddCategory.Visible = true;
            }
            catch
            {
                lbThongBaoAddCategory.Text = "Add categories error.";
                lbThongBaoAddCategory.CssClass = "error";
                lbThongBaoAddCategory.Visible = true;
            }
            lbLimitQuantity.Text = MaxQuantity - CategoryListBox.Items.Count + "";
            this.mpeDanhMuc.Show();
        }
        protected void btnCancel_OnClick(object sender, EventArgs e)
        {
            lbThongBaoAddCategory.Visible = false;
            lbThongBaoAddCategory.Text = "";
        }
        protected void btRemoveSelect_Click(object sender, EventArgs e)
        {
            if (CategoryListBox.SelectedItem != null)
            {
                int index = CategoryListBox.SelectedIndex;
                CategoryListBox.Items.RemoveAt(index);
            }
        }
    }
}